package com.tanhua.dubbo.api.mongo;
import com.tanhua.domain.mongo.LikeVoiceRecord;
import com.tanhua.domain.mongo.UserVoice;
import com.tanhua.domain.mongo.Voice;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.List;

/**
 * @author xie
 * @date 2021/8/22
 */
@Service
public class UserVoiceApiImpl implements UserVoiceApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 随机匹配语音值 过滤掉性别等参数
     * @return
     */
    @Override
    public UserVoice acceptVoice(Long currentUserId,String currentUserGender) {
        //根据记录范围调用随机数
        Query query = new Query();
        //查询条件：不等于当前用户对象的性别
        query.addCriteria(Criteria.where("gender").ne(currentUserGender));
        //计算一周前的时间
        long time = new Date().getTime()-(60*60*24*7*1000);
        //限制时间为7天内
        query.addCriteria(Criteria.where("created").gte(time));
        //查询所有符合条件的记录数,获取记录数量
        long count = mongoTemplate.count(query, Voice.class);
        query.skip(RandomUtils.nextLong(1, count)-1).limit(10);
        //获取前10条的记录 如果有一个符合条件 就直接返回
        List<UserVoice> userVoices = mongoTemplate.find(query, UserVoice.class);
        //再创建一个query2条件 用于语音记录表
        //遍历
        for (UserVoice userVoice : userVoices) {
            Query query2 = new Query();
            //根据语音的id和用户的Id查询 喜欢语音记录表
            query2.addCriteria(Criteria.where("userId").is(String.valueOf(currentUserId)).and("voiceId").is(userVoice.getId()));
            LikeVoiceRecord records = mongoTemplate.findOne(query2, LikeVoiceRecord.class);
            //如果记录不存在 那么就返回这个userVoice
            if (ObjectUtils.isEmpty(records)) {
                //返回之前先插入语音记录表
                LikeVoiceRecord likeVoiceRecord = new LikeVoiceRecord();
                likeVoiceRecord.setUserId(currentUserId + "");//当前用户
                likeVoiceRecord.setVoiceId(userVoice.getId());//语音Id
                likeVoiceRecord.setIsLike(1 + "");//默认喜欢
                mongoTemplate.save(likeVoiceRecord);
                //返回语音对象
                return userVoice;
            }
            else {
                //记录存在 那么就跳过这次循环
                continue;
            }
        }
        //如果循环中都没有查询到数据 那么就递归调用该方法
        return acceptVoice(currentUserId, currentUserGender);
    }

       /* //获得一个语音对象 这个对象可能为null
        UserVoice userVoice = mongoTemplate.findOne(query, UserVoice.class);
        //没找到匹配的语音 递归调用方法继续查询
        if(ObjectUtils.isEmpty(userVoice)){
          return acceptVoice(gender);
        }
        //如果不为Null 再根据语音的Id查询记录表是否存在记录
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("voiceId").is(userVoice.getId()));
        LikeVoiceRecord likeVoiceRecord = mongoTemplate.findOne(query1, LikeVoiceRecord.class);
        //如果语音记录存在 那么就继续调用方法
        if(!ObjectUtils.isEmpty(likeVoiceRecord)){
            return acceptVoice(gender);
        }else {
            //语音记录不存在 就返回匹配的语音对象
            LikeVoiceRecord likeVoiceRecord1 = new LikeVoiceRecord();
            likeVoiceRecord1.setUserId(userVoice.getUserId()+"");//用户Id
            likeVoiceRecord1.setIsLike(1+"");//默认喜欢了
            likeVoiceRecord1.setVoiceId(userVoice.getId());//语音Id
            mongoTemplate.insert(likeVoiceRecord1);
            return userVoice;
        }
*/

}
